package zjh.msg.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import zjh.msg.entity.Permission;
import zjh.msg.service.UserService;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping("login")
    public String login(Permission permission, HttpSession session, Model model) {
//        System.out.println("前端拿到的登录信息"+permission);
        Permission loginUser = userService.login(permission);
        // 添加调试信息
//        System.out.println("(UserController.java：/user/login)loginUser: " + loginUser);

        if(loginUser!=null){
            session.setAttribute("user",loginUser);
            return "redirect:/msg";
        }
        model.addAttribute("error","账号或密码错误");
        return "forward:/pages/login.jsp";
    }

    @GetMapping("logout")
    public String logout(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/msg";
    }
}
